
cc.Class({
    extends: cc.Component,

    properties: {
        objItemBg: cc.Sprite,
        //显示关数
        labelLevelNumber: cc.Label,

        objIsPass: cc.Node,
        objStarList:cc.Node,

        spritesItemBg:[cc.SpriteFrame],
        //存储数据关数
        _levelInfo: null,

    },
    
    setListItemInfo(itemInfo, index,itemBgTypeByScene) {
        //根据场景跟换底
        this.objItemBg.spriteFrame=this.spritesItemBg[itemBgTypeByScene.get(itemInfo.area)];

        this.labelLevelNumber.string = (index + 1);
        this._levelInfo = itemInfo;
        let isPass = (index + 1) <= parseInt(window.gameConfig.gameData.level)
        var levelEndDataArr = window.gameConfig.gameData.levelEndData.split(",");

        this.objIsPass.active = (index+2)<= parseInt(window.gameConfig.gameData.level);
        cc.log("第几关:", index + 1, "是否通过:", isPass)

        this.setIsShowLevelNumber(isPass);
        cc.log("数量:",levelEndDataArr[itemInfo.level-1],"下标:",itemInfo.level-1);
        this.showStar(levelEndDataArr[itemInfo.level-1]);
    },

    setIsShowLevelNumber(isPass) {
        this.objItemBg.node.color = isPass ? new cc.Color(255, 255, 255) : new cc.Color(80, 80, 80);
       
    },

    levlItemClick() {
        window.audioMgr.playGameAudio("buttonCheck")
        //如果大于下一关直接返回
        if (this._levelInfo.level > window.gameConfig.gameData.level) {
            return;
        } else if (window.gameConfig.gameData.coin < 2) {
            return
        }

        //玩家进入关卡扣除2个体力值
        var coin = parseInt(window.gameConfig.gameData.coin) - 2;
        window.gameConfig.saveGameData("coin", coin);

        cc.log("点击选关  选择关卡信息" + this._levelInfo.level);
        window.gameLevelNum = this._levelInfo.level;
        cc.director.loadScene("gameMain");
    },

    showStar(num){
        if(num>0){
            //显示星星数量
            for (let index = 0; index < num; index++) {
                this.objStarList.getChildByName("star_win_"+index).active=true;
            }
        }
        //如果大于零显示星星
        this.objStarList.active=num>0;

    }
});